# 图形化界面管理窗
import os
import threading

import pyautogui
import requests
import ttkbootstrap as ttk

import software_path
import variable
import SoftWareUpdate as swu
import software_install as si
from tkinter import scrolledtext
import clear_wxwork_global as cwg
import admin_Option


def initialization():
    """初始化"""
    try:
        if not os.path.isfile(variable.PsExec_Path):
            psexec = requests.get(variable.PsExec_url_path)
            if psexec.status_code == 404:
                print('插件下载失败，检查链接是否正常')
            else:
                open(variable.PsExec_Path, 'wb').write(psexec.content)
        if not os.path.isfile(variable.WxWork_dk_path):
            wxdk = requests.get(variable.WxWork_dk_path)
            if wxdk.status_code == 404:
                print('插件下载失败，检查链接是否正常')
            else:
                open(variable.PsExec_Path, 'wb').write(wxdk.content)

        if not (os.path.isfile(variable.Activate_System_12F_Path) and os.path.isfile(
                variable.Activate_System_17F_Path)):
            jh17 = requests.get(variable.Activate_System_17F_url)
            jh12 = requests.get(variable.Activate_System_12F_url)
            if jh17.status_code == '404' or jh12.status_code == '404':
                print('插件下载失败，检查链接是否正常')
            else:
                open(variable.Activate_System_17F_Path, 'wb').write(jh17.content)
                open(variable.Activate_System_12F_Path, 'wb').write(jh12.content)

    except Exception as e:
        print(e)


# 执行初始化
initialization()


def admin_option():
    admin_Option.admin_gui()

    pyautogui.alert(title='信息', text='功能开发中')


def hngjx_Update():
    """
    工具箱更新
    :return:
    """
    # 更新程序 hntools_update.exe http://192.168.65.50/hntools_update.exe
    response = requests.get(variable.hntools_update_url)

    if response.status_code == 404:
        ts.insert('end', '目前没有版本更新!!!')
    else:
        # 调用更新进程进行更新
        #open(variable.hntools_update_path, 'wb').write(response.content)
        swu.SoftWareUpdate(update_url=variable.hntools_update_url).execution_software(variable.hntools_update_path)
        #os.system('start ' + variable.hntools_update_path)


def Software_push():
    """软件推送"""
    r = requests.get(variable.hn_software)
    if r.status_code == 404:
        pyautogui.alert(title='信息', text='目前没有公司软件推送')
    else:
        ts.insert('end', '软件下载完成，请注意安装程序弹窗')
        swu.SoftWareUpdate(update_url=variable.hn_software).software_install_thread()


def WxChat_Update():
    """
    微信更新
    """
    ts.delete(1.0, 'end')
    ts.insert('end', '软件下载中......' + '\n')
    ts.insert('end', '微信下载链接:' + variable.WXCHATUPDATE_URL + '\n')
    swu.SoftWareUpdate(variable.ADMIN_PASSWORD,variable.WXCHATUPDATE_URL).download_software()


def DingTalk_Update():
    """
    钉钉软件更新
    """
    swu.SoftWareUpdate().execution_software(variable.DINGTALKUPDATE_URL)


def WxWork_upgrade():
    """
    企微升级级
    :return:
    """
    ts.delete(1.0, 'end')
    ts.insert('end', '软件下载中......' + '\n')
    ts.insert('end', '企微下载链接:' + variable.WxWork_Upgrade_url + '\n')
    swu.SoftWareUpdate(variable.ADMIN_PASSWORD,variable.WxWork_Upgrade_url).software_install_thread()


def WxWork_dk():
    """
    调用企微多开程序
    :return:
    """

    s1 = os.system('start ' + variable.WxWork_dk_path)
    if s1 == 1:
        try:
            get_qwdk = requests.get(variable.WxWork_dk_url)
            open(variable.WxWork_dk_path, 'wb').write(get_qwdk.content)
            ts.insert('end', 'qwdk下载完成' + '\n')
            os.system('start ' + variable.WxWork_dk_path)
        except Exception as e:
            ts.insert('end', '无法启动qwdk.exe，请检查是否有此文件' + '\n' + '报错信息：' + str(e) + '\n')


def WxWork_dk_Thread():
    """
    创建企微多开线程
    """
    t = threading.Thread(target=WxWork_dk())
    t.start()


def WxWork_lxq():
    """
    调用企微连选程序
    """
    s1 = os.system('start ' + variable.WxWork_lxq_lj)
    if s1 == 1:
        get_qwdk = requests.get(variable.Wxwork_lxq_url)
        open(variable.WxWork_lxq_lj, 'wb').write(get_qwdk.content)
        print('lxq下载完成')
        os.system('start ' + variable.WxWork_lxq_lj)


def Start_SunloginClient():
    """
    使用管理员权限启动向日葵
    """
    ts.delete(1.0, 'end')
    print(variable.SUNLOGINCLIENT_PATH)
    if os.path.isfile(variable.SUNLOGINCLIENT_PATH):
        ts.insert('end', '启动向日葵' + '\n')
        ts.insert('end', '默认路径：' + str(variable.SUNLOGINCLIENT_PATH))
        swu.SoftWareUpdate().execution_software(variable.SUNLOGINCLIENT_PATH)

    else:
        ts.insert('end', '向日葵未安装，或者默认安装路径错误')


def Start_ym():
    """
    使用管理员权限启动益盟
    """
    ts.delete(1.0, 'end')
    if os.path.isfile(variable.YM_PATH):
        print('存在')
        ts.insert('end', '启动益盟' + '\n')
        ts.insert('end', '默认路径：' + str(variable.YM_PATH) + '\n')
        swu.SoftWareUpdate().execution_software(variable.YM_PATH)
    else:
        ts.insert('end', '益盟未安装，或者默认安装路径错误')


def WeChat_dk():
    """
    微信多开
    """
    WeChat_Path = software_path.Get_WeChat_Path()
    if WeChat_Path is None:
        os.system(r'"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"')
    else:
        ts.insert('end', '微信启动路径：' + WeChat_Path)
        ts.insert('end', '获取微信启动路径：' + WeChat_Path + '\n')
        # 多开六个个微
        os.system(WeChat_Path)


def WeChat_Dk_Thead():
    """
    微信多开线程
    """
    ts.delete(1.0, 'end')
    # i = 0
    # while i <= 5:
    #     wx = threading.Thread(target=WeChat_dk, daemon=False)
    #     wx.start()
    #     i += 1
    os.system('start c:\\poi\\wxdk.bat')


def Computer_Configuration():
    """
    电脑配置信息
    :return:
    """
    computer_msg = software_path.Get_Computer_Configuration()
    ts.delete(1.0, 'end')
    for i in computer_msg:
        ts.insert('end', str(i) + '\n')
        print(i)


def Activate_System():
    """激活系统"""
    ping_test = os.system('ping 192.168.65.92')
    if ping_test == 0:
        if os.path.isfile(variable.Activate_System_12F_Path):
            r = os.system('runas /user:sysadmin /savecred' + ' ' + variable.Activate_System_12F_Path)
            if r != 0:
                os.system('runas /user:PC000 /savecred' + ' ' + variable.Activate_System_12F_Path)
    else:
        if os.path.isfile(variable.Activate_System_17F_Path):
            r = os.system('runas /user:PC000 /savecred' + ' ' + variable.Activate_System_17F_Path)
            if r != 0:
                os.system('runas /user:sysadmin /savecred' + ' ' + variable.Activate_System_17F_Path)


def clear_global():
    """清理企微global"""

    pc = pyautogui.confirm(text='运行此程序将会退出所有企业微信', title='警告', buttons=['OK', 'Cancel'])
    if pc == 'OK':
        try:
            threading.Thread(target=cwg.clear_wxwork())
        except Exception as e:
            print(e)


# 创建GUI对象窗口
root = ttk.Window(themename="superhero")
root.title('海能运维工具箱v3.0')
root.geometry('560x400')

# root.resizable(0, 0)
# ttk.Combobox(root).grid(row=1, column=2)y

ttk.Button(root, bootstyle="info", command=hngjx_Update, text='更新').grid(row=2, column=1, pady=120, ipadx=5,
                                                                         )
ttk.Button(root, bootstyle="info", command=admin_option, text='管理员').grid(row=2, column=2, pady=120, )
ttk.Button(root, bootstyle="info", command=Computer_Configuration, text='电脑配置').grid(row=2, column=3, columnspan=1,
                                                                                     pady=120, sticky='W')
ttk.Button(root, bootstyle="info", command=Activate_System, text='系统激活').grid(row=2, column=3, columnspan=2,
                                                                              pady=120)
ttk.Button(root, bootstyle="info", command=clear_global, text='清理企微').grid(row=2, column=3, columnspan=2,
                                                                           pady=120, sticky='E')

# 软件启动面板
frame1 = ttk.Labelframe(root, bootstyle="info", text='软件启动')
ttk.Button(frame1, text='企微多开', command=WxWork_dk_Thread, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
ttk.Button(frame1, text='企微连选器', command=WxWork_lxq, bootstyle="success").pack(pady=5, padx=10, ipadx=1)
ttk.Button(frame1, text='启动向日葵', command=Start_SunloginClient, bootstyle="success").pack(pady=5, padx=10,
                                                                                         ipadx=1)
ttk.Button(frame1, text='启动益盟', command=Start_ym, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
ttk.Button(frame1, text='微信多开', bootstyle="success", command=WeChat_Dk_Thead).pack(pady=5, padx=10, ipadx=7)
frame1.grid(row=0, column=1)

# 软件更新面板
frame2 = ttk.Labelframe(root, bootstyle="info", text='软件更新')
ttk.Button(frame2, text='微信更新', command=WxChat_Update, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
ttk.Button(frame2, text='钉钉更新', command=DingTalk_Update, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
ttk.Button(frame2, text='企微升级', command=WxWork_upgrade, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
ttk.Button(frame2, text='软件安装', command=si.get_gui, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
ttk.Button(frame2, text='软件推送', command=Software_push, bootstyle="success").pack(pady=5, padx=10, ipadx=7)
frame2.grid(row=0, column=2, padx=5)
# 程序执行信息模块
frame3 = ttk.Labelframe(root, bootstyle="info", text='程序执行信息')
ts = scrolledtext.ScrolledText(frame3, width=36, height=11)
ts.insert('end','--海能工具箱v3.0--'+'\n')
ts.insert('end', '1.使用微信多开需要提前关闭微信,可以开5个' + '\n')
ts.insert('end', '2.企微升级是官网最新版本,下载安装需要时间所以需要等几十秒' + '\n')
ts.insert('end', '3.使用连选器需要先卡bug' + '\n')
ts.insert('end','4.企微清理功能只清理企微global文件夹'+'\n')

ts.pack()

frame3.grid(row=0, column=3)

root.mainloop()
